package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcOrder;
import com.yxinsur.imc.business.domain.ImcOrderRiskEndorse;
import com.yxinsur.imc.business.domain.ImcRepoIns;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.enums.YesOrNoEnum;
import com.yxinsur.imc.business.form.OrderEndorseForm;
import com.yxinsur.imc.business.form.OrderRiskEndorseForm;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.mapper.ImcOrderRiskEndorseMapper;
import com.yxinsur.imc.business.mapper.ImcRepoInsMapper;
import com.yxinsur.imc.business.service.IImcOrderRiskEndorseService;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 保单险种批单详情Service业务层处理
 *
 * @author imc
 * @date 2021-10-09
 */
@Service
public class ImcOrderRiskEndorseServiceImpl extends ServiceImpl<ImcOrderRiskEndorseMapper, ImcOrderRiskEndorse> implements IImcOrderRiskEndorseService {
    @Autowired
    private ImcOrderMapper imcOrderMapper;
    @Autowired
    private ImcRepoInsMapper imcRepoInsMapper;
    @Autowired
    private ImcOrderRiskEndorseMapper imcOrderRiskEndorseMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 新增保单险种批单
     *
     * @param endorseForm 批单信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult addImcOrderRiskEndorse(OrderEndorseForm endorseForm) {
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        ImcOrderRiskEndorseMapper rm = session.getMapper(ImcOrderRiskEndorseMapper.class);
        Long orderId = endorseForm.getOrderId();
        ImcOrder order = this.imcOrderMapper.selectById(orderId);

        // 删除险种信息
        List<ImcOrderRiskEndorse> oldRiskEndorseList = this.imcOrderRiskEndorseMapper.listOrderRiskEndorseByOrderId(orderId);
        if (CollUtil.isNotEmpty(oldRiskEndorseList)) {
            for (ImcOrderRiskEndorse item : oldRiskEndorseList) {
                item.setUpdateTime(DateUtil.date());
                item.setUpdateId(SecurityUtils.getUsername());
                rm.deleteImcOrderRiskEndorseById(item);
            }
        }

        // 保存险种信息
        List<OrderRiskEndorseForm> riskEndorseFormList = endorseForm.getRiskEndorseFormList();
        String serialId = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        List<ImcOrderRiskEndorse> riskEndorseList = saveRiskEndorse(riskEndorseFormList, order, serialId);
        for (ImcOrderRiskEndorse riskEndorse : riskEndorseList) {
            rm.insertImcOrderRiskEndorse(riskEndorse);
        }
        session.commit();
        return AjaxResult.success();
    }

    /**
     * 保存保单险种批改
     *
     * @param riskEndorseFormList 险种信息
     * @param order               保单信息
     * @param serialId            批单流水号
     * @return List<ImcOrderRiskEndorse>
     */
    private List<ImcOrderRiskEndorse> saveRiskEndorse(List<OrderRiskEndorseForm> riskEndorseFormList, ImcOrder order, String serialId) {

        List<ImcOrderRiskEndorse> riskEndorseList = CollUtil.newArrayList();
        for (OrderRiskEndorseForm item : riskEndorseFormList) {
            ImcOrderRiskEndorse riskEndorse = new ImcOrderRiskEndorse();
            BeanUtil.copyProperties(item, riskEndorse, CopyOptions.create().ignoreCase().setIgnoreNullValue(true));

            riskEndorse.setSerialId(serialId);
            riskEndorse.setOrderId(order.getId());
            // 保单生效日期
            riskEndorse.setEffectiveDate(order.getEffectiveDate());
            // 保单失效日期
            riskEndorse.setExpiredDate(order.getExpiredDate());
            // 回执录入时间(回执录入系统时间)
            riskEndorse.setCustAckDate(order.getCustAckDate());
            // 客户回执签收时间
            riskEndorse.setAckDate(order.getAckDate());
            // 险种终止日期
            riskEndorse.setEndDate(order.getEndDate());
            // 终止原因[A犹豫期退保B退保C理赔终止
            riskEndorse.setEndCause(order.getEndCause());
            // 险种终止备注
            riskEndorse.setEndCauseNote(order.getEndCauseNote());
            ImcRepoIns ins = this.imcRepoInsMapper.selectById(item.getRepoInsId());
            riskEndorse.setRepoInsId(ins.getId());
            riskEndorse.setInsCode(ins.getInsCode());
            riskEndorse.setRiskName(ins.getRiskName());

            //默认第一年
            riskEndorse.setPolicyYear(1L);
            //默认第一次
            riskEndorse.setPayNum(1L);
            // 首年则相等
            if (riskEndorse.getPolicyYear() == 1L) {
                riskEndorse.setPayablePrem(riskEndorse.getPrem());
                riskEndorse.setPaymentPrem(riskEndorse.getPrem());
            }
            // 折标率系数默认1.0
            riskEndorse.setStandRateFactor(new BigDecimal("1.0"));

            riskEndorse.setIsFeeSettled(YesOrNoEnum.N.name());
            riskEndorse.setIsCommPaid(YesOrNoEnum.N.name());
            riskEndorse.setRiskStatusCode(order.getOrderStatusCode());
            riskEndorse.setApplyDate(order.getApplyDate());
            riskEndorse.setInsureDate(order.getInsureDate());

            riskEndorse.setCreateId(SecurityUtils.getUsername());
            riskEndorse.setCreateTime(DateUtil.date());
            riskEndorse.setUpdateId(SecurityUtils.getUsername());
            riskEndorse.setUpdateTime(DateUtil.date());
            riskEndorse.setTenantId(SecurityUtils.getTenantId());
            riskEndorse.setIsDeleted(IsDeletedEnum.N.getCode());
            riskEndorseList.add(riskEndorse);
        }
        return riskEndorseList;
    }
}
